package cn.farm.farm.Service.CommonService;

import cn.farm.farm.Config.Common.CommonDto.DiseaseExecution;
import cn.farm.farm.Config.Common.CommonDto.PageHelperResult;
import cn.farm.farm.Config.Common.CommonDto.Result;
import cn.farm.farm.Config.Common.CommonEnums.DiseaseStateEnum;
import cn.farm.farm.Config.Common.CommonException.LikeException;
import cn.farm.farm.Entity.Common.Diseasestress;
import cn.farm.farm.Entity.Common.DiseasestressSon;
import cn.farm.farm.Mapper.CommonMapper.DiseaseMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DiseaseService {
    @Autowired
    private DiseaseMapper diseaseMapper;


    public Result<PageHelperResult> foundByName(String name, Integer pageNum, Integer pageSize) {
        //声明数据变量
        Result<PageHelperResult> result;//返回信息
        DiseaseExecution execution;//返回Data
        PageHelperResult<DiseasestressSon> pageHelperResult = new PageHelperResult<>();
        try{
            //查询所有数据
            List<DiseasestressSon> list = diseaseMapper.likeSelect(name);
            Integer all = list.size();
            Integer Size;
            //查询数据库
            PageHelper.startPage(pageNum,pageSize);//从第几条数据开始，每页显示多少条数据
            list = diseaseMapper.likeSelect(name);
            if(pageNum<=(Integer)(all/pageSize)){
                Size = pageSize;
            }else{
                Size = all - (pageNum-1)*pageSize;
            }
            pageHelperResult.setList(list);
            pageHelperResult.setNumber(all);
            pageHelperResult.setName(name);
            pageHelperResult.setPageNumber(Size);
            if(list.size() == 0){
                throw new LikeException("查询不到任何信息");
            }

            //构造返回变量
            execution = new DiseaseExecution(DiseaseStateEnum.SUCCESS);
            result = new Result(execution.getState(),pageHelperResult,execution.getStateInfo());

            return result;
        }catch(LikeException l){
            execution = new DiseaseExecution(DiseaseStateEnum.FOUND_NULL);
            result = new Result(execution.getState(),pageHelperResult,execution.getStateInfo());

            return result;
        }
    }
}
